/**
 * 
 */
package cc.rico.jms.p2p.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author rico 2016年8月2日
 *
 */
public class QueueMessageListener implements MessageListener {
	private static final Logger logger = LoggerFactory.getLogger(QueueMessageListener.class);
	private Session session;
	private int index=1;
	
    public QueueMessageListener(Session session){  
        this.session = session;  
    }  
    
    public QueueMessageListener(Session session, int index){  
        this.session = session;  
        this.index = index;
    }  

	/* (non-Javadoc)
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	@Override
	public void onMessage(Message message) {
		if(message == null) {
			logger.info("message is null");
			return;
		}
		
		try {
			if(message instanceof TextMessage) {
				logger.info("consumer={}, message={}", index, ((TextMessage)message).getText());
			} else {
				logger.info("message={}", message);
			}
			
//			Destination replyTo = message.getJMSReplyTo();
//			if(replyTo != null) {
//				message.clearBody();  
//				
//				MessageProducer producer = session.createProducer(replyTo);
//				producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//				producer.setTimeToLive(5000);
//				producer.send(message);
//				producer.close();
//			}
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		
	}

}
